﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.ComponentModel.Composition;
using ShredTheLog.Api;

namespace ShredTheLog.Converters
{
    [ValueConversion(typeof(Country?), typeof(object))]
    public sealed class NullableCountryConverter : ConverterBase, IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var v = (Country?)value;
            return v.HasValue ? (object)v.Value : (object)new NullValue();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is Country)
                return (Country?)value;
            else if (value is NullValue)
                return (Country?)null;
            else
                throw new NotSupportedException();
        }

        public static readonly NullableCountryConverter Default = new NullableCountryConverter();
    }
}
